<?php

namespace plugins\alarm\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\alarm\model\AlarmModel;
use plugins\alarm\model\TeamModel;
use plugins\CommonUntil;
use think\Db;

/**
 * 监测报警
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 监测报警列表
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 时间轴页面
     */
    public function time_axis()
    {
        return $this->fetch();
    }

    /**
     * 确认页面
     */
    public function edit_page()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }

    /**
     * 详情
     */
    public function detail_page()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }


    /**
     * 监测报警分页列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $alarmModel = new AlarmModel();
        $pageSize = '10';
        if (!empty($param['pageSize'])) {
            $pageSize = $param['pageSize'];
        }
        $where = [];
        if (!empty($param['time'])) {
            $where['monitor_time'] = ['between time',$param['time']];
        }
        if (!empty($param['detail_status'])) {
            $where['detail_status'] = $param['detail_status'];
        }
        //致灾因子
        if(!empty($param['disaster_cause'])){
            $where['disaster_cause'] = ['in',$param['disaster_cause']];
        }
        //报警等级
        if(!empty($param['alarm_level'])){
            $where['alarm_level'] = ['in',$param['alarm_level']];
        }
        //报警类型
        if(!empty($param['alarm_type'])){
            $where['alarm_type'] = ['in',$param['alarm_type']];
        }
        //过火面积
        if(!empty($param['burned_area'])){
            if(is_array($param['burned_area']) && count($param['burned_area'])>1 && $param['burned_area'][1]>0){
                $where['burned_area'] = [['>', $param['burned_area'][0]], ['<', $param['burned_area'][1]],'and'];
            }
        }
        //地区
        if(!empty($param['cri_code'])){
            $where['cri_code'] = ['in',$param['cri_code']];
        }

        $list = $alarmModel->where($where)->paginates($pageSize)->each(function ($item) use ($alarmModel) {
            return $alarmModel->getDetail($item);
        });
        return api_json_echo(true, '查询成功', $list);
    }

    /**
     * 获取详情
     */
    public function getDetail()
    {
        $param = $this->request->param();
        $alarmModel = new AlarmModel();
        if (empty($param['id'])) {
            return api_json_echo(false, '传参错误');
        }
        $info = $alarmModel->where('id', $param['id'])->find();
        if(empty($info)){
            return api_json_echo(false, '查询失败');
        }
        if(!empty($info['monitor_time'])){
            $info['monitor_time'] = date('Y-m-d H:i:s',$info['monitor_time']);
        }
        if(!empty($info['deal_time'])){
            $info['deal_time'] = date('Y-m-d H:i:s',$info['deal_time']);
        }
        //处理人
        $deal_name = '';
        if(!empty($info['deal_uid'])){
            $user = Db::name('user')->where('id',$info['deal_uid'])->find();
            if(!empty($user)){
                $deal_name = $user['user_login'];
            }
        }
        $info['deal_name'] = $deal_name;
        //应急部门
        $team_name = '';
        if(!empty($info['team_id'])){
            $team = Db::name('team')->where('id',$info['team_id'])->find();
            if(!empty($team)){
                $team_name = $team['name'];
            }
        }
        $info['team_name'] = $team_name;
        return api_json_echo(true, '查询成功', $info);
    }

    /**
     * 获取救援队列表
     */
    public function getTeamList(){
        $param = $this->request->param();
        $teamModel = new TeamModel();
        $where = [];
        if(!empty($param['type'])){
            $where['type'] = $param['type'];
        }
        if(!empty($param['distance'])){
            $where['distance'] = ['<=',$param['distance']];
        }
        $list = $teamModel->where($where)->select();
        return api_json_echo(true, '查询成功', $list);
    }

    /**
     * 误报处理
     */
    public function misstatePost(){
        $param = $this->request->param();
        if(empty($param['id'])){
            return api_json_echo(false, '未获取到参数');
        }
        $alarmModel = new AlarmModel();
        $param['deal_status'] = 2;
        $param['deal_time'] = time();
        $param['deal_uid'] = cmf_get_current_admin_id();
        $res = $alarmModel->where('id',$param['id'])->strict(false)->update($param);
        if($res){
            return api_json_echo(true, '提交成功');
        }
        return api_json_echo(false, '提交失败');
    }

    /**
     * 确认处理
     */
    public function makeSurePost(){
        $param = $this->request->param();
        if(empty($param['id'])){
            return api_json_echo(false, '未获取到参数');
        }
        $alarmModel = new AlarmModel();
        $param['deal_status'] = 1;
        $param['deal_time'] = time();
        $param['deal_uid'] = cmf_get_current_admin_id();
        $res = $alarmModel->where('id',$param['id'])->strict(false)->update($param);
        if($res){
            return api_json_echo(true, '提交成功');
        }
        return api_json_echo(false, '提交失败');
    }




    /**
     * 删除
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['id'])) {
            return api_json_echo(false, '传值错误');
        }
        $alarmModel = new AlarmModel();
        $re = $alarmModel->where('id','in', $param['id'])->delete();
        if (empty($re)) {
            return api_json_echo(false, '删除失败');
        }
        return api_json_echo(true, '删除成功');
    }

    /**
     * 导出excel
     */
    public function exportExcel()
    {
        $commonUntil = new CommonUntil();

        $alarmModel = new AlarmModel();
        $where = [];
        $list = $alarmModel->where($where)->select()->each(function ($item) use ($alarmModel) {
            return $alarmModel->getDetail($item);
        });
        $expDataList = [];
        foreach ($list as $item){
            $item_list = [];
            $item_list[] = $item['monitor_time'];
            $item_list[] = $item['coordinate'];
            $item_list[] = $item['alarm_type'];
            $item_list[] = $item['alarm_level'];
            $item_list[] = $item['burned_area'];
            $item_list[] = $item['disaster_cause'];
            $item_list[] = $item['alarm_address'];
            $item_list[] = $item['alarm_num'];
            $item_list[] = $item['detail_status_str'];
            $item_list[] = $item['monitor_device'];
            $item_list[] = $item['device_info'];
            $item_list[] = $item['video_line'];
            $expDataList[] = $item_list;
        }
        $expTitle = date('Y-m-d H:i').'-设备导出';
        $expHeadArr = ['监测时间','坐标','报警类型','报警等级','过火面积','致灾因子','报警地点','报警次数','处理状态','监测设备','设备情况','视频连线'];
        $filePath = $commonUntil->exportExcelUntil($expTitle,$expHeadArr,$expDataList);
        return api_json_echo(true, '导出成功', zy_domain() . '/upload/' . $filePath);
    }
}
